package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.MachineShow;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.MachineShowRepository;
import co.yixiang.modules.shop.service.MachineShowService;
import co.yixiang.modules.shop.service.dto.MachineShowDto;
import co.yixiang.modules.shop.service.dto.MachineShowQueryCriteria;
import co.yixiang.modules.shop.service.mapper.MachineShowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijunze
* @date 2020-04-25
*/
@Service
//@CacheConfig(cacheNames = "machineShow")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MachineShowServiceImpl implements MachineShowService {

    private final MachineShowRepository machineShowRepository;

    private final MachineShowMapper machineShowMapper;

    public MachineShowServiceImpl(MachineShowRepository machineShowRepository, MachineShowMapper machineShowMapper) {
        this.machineShowRepository = machineShowRepository;
        this.machineShowMapper = machineShowMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(MachineShowQueryCriteria criteria, Pageable pageable){
        Page<MachineShow> page = machineShowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(machineShowMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<MachineShowDto> queryAll(MachineShowQueryCriteria criteria){
        return machineShowMapper.toDto(machineShowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public MachineShowDto findById(Long id) {
        MachineShow machineShow = machineShowRepository.findById(id).orElseGet(MachineShow::new);
        ValidationUtil.isNull(machineShow.getId(),"MachineShow","id",id);
        return machineShowMapper.toDto(machineShow);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public MachineShowDto create(MachineShow resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return machineShowMapper.toDto(machineShowRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(MachineShow resources) {
        MachineShow machineShow = machineShowRepository.findById(resources.getId()).orElseGet(MachineShow::new);
        ValidationUtil.isNull( machineShow.getId(),"MachineShow","id",resources.getId());
        machineShow.copy(resources);
        machineShowRepository.save(machineShow);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            machineShowRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<MachineShowDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MachineShowDto machineShow : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", machineShow.getName());
            map.put("价格", machineShow.getPrice());
            map.put("分类id", machineShow.getTypeId());
            map.put("分类名称", machineShow.getTypeName());
            map.put("轮播图", machineShow.getSliderImage());
            map.put("描述", machineShow.getStoreInfo());
            map.put("品牌id", machineShow.getBrandId());
            map.put("品牌名称", machineShow.getBrandName());
            map.put("重量", machineShow.getWeight());
            map.put("高度", machineShow.getHeight());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}